#pragma
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include"Log.hpp"
const static std::string sep = ": ";
class Dict
{
public:
    void LoadDict()
    {
        std::ifstream in(_path);
        if(!in.is_open())
        {
            LOG(FATAL, "open %s failed\n", _path);
            exit(1);
        }
        std::string line;
        while (std::getline(in,line))
        {
            LOG(INFO, "load info:%s\n", line);
            if(line.empty())
            continue;
            auto pos = line.find(sep);
            if(pos==std::string::npos)
                continue;
            std::string key = line.substr(0, pos);
            if(key.empty())
            continue;
            std::string value = line.substr(pos+sep.size());
            if(value.empty())
            continue;
            _dict.insert(std::make_pair(key, value));
        }
        LOG(INFO, "load dict success\n");
        in.close();
    }

public:
    Dict(const std::string& path):_path(path) 
    {
        LoadDict();
    }
    ~Dict() {}
    std::string Translate(std::string word)
    {
        if(word.empty())
            return "None";
        auto iter = _dict.find(word);
        if(iter==_dict.end())
            return "Unfind";
        else
            return iter->second;
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _path;
};